using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using DataObject.Core;
using DataObject.Database;

namespace DataObject.DataAccess
{
    public partial class ParameterDAO : DataBaseProcessBase<ParameterEntity, OBSDataContext>
    {
        public static ParameterDAO CreateInstant()
        {
            return new ParameterDAO();
        }

        #region Extensibility Method Declarations
        public void InsertCollection(List<ParameterEntity> ls)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                db.ParameterEntities.InsertAllOnSubmit(ls);
                db.SubmitChanges();
            }
        }
        public void Update(ParameterEntity item)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                db.UpdateParameter(item.ParamID, item.GroupName, item.ShortValue, item.Value, item.AllowedModify, item.Active);
            }
        }
        public List<ParameterEntity> GetParameterAll()
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.ParameterEntities.ToList();
            }
        }
        public ParameterEntity GetParameterById(int pParamID)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.ParameterEntities.SingleOrDefault(a => a.ParamID == pParamID);
            }
        }
        public List<ParameterEntity> SelectListItem(int? ParamID, string GroupName, string ShortValue, string Value, bool? AllowedModify, bool? Active, HashSet<string> SortClause)
        {
            int Count = 0;
            return SelectListItem(ParamID, GroupName, ShortValue, Value, AllowedModify, Active, null, null, out Count, SortClause);
        }
        public List<ParameterEntity> SelectListItem(int? ParamID, string GroupName, string ShortValue, string Value, bool? AllowedModify, bool? Active, int? Page, int? Row, out int Count, HashSet<string> SortClause)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var query = (from table in db.ParameterEntities select table);
                // -- Nullable --
                if (ParamID.HasValue)
                    query = query.Where(table => table.ParamID == ParamID.Value);
                if (AllowedModify.HasValue)
                    query = query.Where(table => table.AllowedModify == AllowedModify.Value);
                if (Active.HasValue)
                    query = query.Where(table => table.Active == Active.Value);
                // -- Empty --
                if (!String.IsNullOrEmpty(GroupName))
                    query = query.Where(table => table.GroupName.ToLower().Equals(GroupName.ToLower()));
                if (!String.IsNullOrEmpty(ShortValue))
                    query = query.Where(table => table.ShortValue.ToLower().Equals(ShortValue.ToLower()));
                if (!String.IsNullOrEmpty(Value))
                    query = query.Where(table => table.Value.ToLower().Equals(Value.ToLower()));

                // -- Sort --
                for (int i = SortClause.Count - 1; i >= 0; i--)
                    query = query.OrderBy(SortClause.ElementAt(i));

                // Execute Linq to SQL
                Count = query.Count();
                if (Page.HasValue && Row.HasValue)
                    return query.PageIQuery(Page.Value, Row.Value).ToList();
                return query.ToList();
            }
        }
        #endregion
    }    
}
